﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.WindowsAzure.ServiceRuntime;
using YourWorldServer;
using TcpServer;

namespace YourWorld.Web
{
    public class WebRole : RoleEntryPoint
    {
        private TcpLib.TcpServer tcpServer;
        private TcpLib.TcpServer policyServer;

        //SocketServer tcpServer;
        //PolicyServer policyServer;

        public override bool OnStart()
        {
            string webServicesLocation = RoleEnvironment.GetConfigurationSettingValue( "WebServicePath" );

            //this.policyServer = new PolicyServer();
            //this.policyServer.StartPolicyServer( RoleEnvironment.CurrentRoleInstance.InstanceEndpoints["PolicyEndPoint"].IPEndpoint );

            //this.tcpServer = new SocketServer( webServicesLocation );
            //this.tcpServer.StartSocketServer( RoleEnvironment.CurrentRoleInstance.InstanceEndpoints["SocketEndPoint"].IPEndpoint );

            //USING CODEPLEX 
            var provider = new GreenWorldServiceProvider( webServicesLocation );
            this.tcpServer = new TcpLib.TcpServer( provider,
                RoleEnvironment.CurrentRoleInstance.InstanceEndpoints[ "SocketEndPoint" ].IPEndpoint );
            this.tcpServer.Start();

            var policyProvider = new SocketPolicyServiceProvider(
                RoleEnvironment.GetConfigurationSettingValue( "PolicyFilePath" )
                );
            this.policyServer = new TcpLib.TcpServer( policyProvider,
                RoleEnvironment.CurrentRoleInstance.InstanceEndpoints[ "PolicyEndPoint" ].IPEndpoint );
            this.policyServer.Start();

            return base.OnStart();
        }

        public override void OnStop()
        {
            this.tcpServer.Stop();
            this.policyServer.Stop();

            //if ( this.policyServer != null )
            //    this.policyServer.StopPolicyServer();

            //if ( this.tcpServer != null )
            //    this.tcpServer.StopSocketServer();

            base.OnStop();
        }
    }
}